package com.mrper.util.http;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.miguo.framework.request.JsonData;
import com.miguo.framework.request.RequestJsonHandler;
import com.miguo.http.HttpClient;
import com.miguo.http.Url;
import com.mrper.model.entry.brand.BrandTypeInfoEntry;
import com.mrper.util.http.listener.OnPublishBrandTypeListener;

import java.util.List;

/**
 * Created by Administrator on 2016/8/23.
 * 公共的网络访问方法
 */

public class HttpCommonUtil {

    /**
     * 获取品牌类型的方法
     * @param listener
     */
    public static void getBrandTypes(final OnPublishBrandTypeListener listener){
        HttpClient.post(Url.toRoute(Url.ExtUrl.BrandType, 1), new RequestJsonHandler() {
            @Override
            public void onRequestFinish(JsonData json) {
                if (json.optInt("status") == 200) {
                    JsonData json_data = json.optJson("data");
                    if(json_data != null && json_data.length() > 0) {
                       List<BrandTypeInfoEntry> types = new Gson().fromJson(json_data.toString(),
                               new TypeToken<List<BrandTypeInfoEntry>>(){}.getType());
                        if(listener != null) listener.onPublish(types);
                    }
                }
            }
        });
    }

}
